package com.example.knowman.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.knowman.ObService.*;
import com.example.knowman.ReturnMessage.Message;
import com.example.knowman.ReturnMessage.MessageBackKnowMainId;
import com.example.knowman.ReturnMessage.MessageExKnowShareKey;
import com.example.knowman.dao.labelCategoryWithStaffNAndKnowCategoryN;
import com.example.knowman.entity.*;
import com.example.knowman.mapper.KnowCategoryMapper;
import com.example.knowman.mapper.KnowShareMapper;
import com.example.knowman.mapper.Log4operateMapper;
import com.example.knowman.mapper.StaffListMapper;
import com.example.knowman.service.IKnowCategoryService;
import com.example.knowman.service.IKnowShareService;
import com.example.knowman.service.ILog4operateService;
import com.example.knowman.service.IStaffListService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class KnowShareServiceImpl extends ServiceImpl<KnowShareMapper, knowShare> implements IKnowShareService {

    private final IStaffListService iStaffListService;

    private final OBKnowMainService obKnowMainService;

    public KnowShareServiceImpl(IStaffListService iStaffListService,OBKnowMainService obKnowMainService) {

        this.iStaffListService = iStaffListService;
        this.obKnowMainService = obKnowMainService;

    }

    @Override
    public MessageExKnowShareKey Insert(Integer knowId, String myStaffCode, String comment) {
        //判断有没有已经存在过知识分享表
        knowShare check = this.lambdaQuery().select()
                .eq(knowShare::getKnowId,knowId)
                .eq(knowShare::getMyStaffCode,myStaffCode)
                .one();

        if(check !=null){
            MessageExKnowShareKey here = new MessageExKnowShareKey();
            here.setCode(100);
            here.setTip("已存在该知识分享表");
            return  here;
        }


        //得到所有发布者的员工信息,得到所有知识对应信息
        Map<String, staffList> SL = iStaffListService.getAll();
        Map<Integer, knowMain> KML = obKnowMainService.getMapAll();

        //得到对应单个知识与发布员工
        knowMain thisKM = KML.get(knowId);
        staffList thisStaffPost = SL.get(thisKM.getStaffCode());


        //新增knowShare
        MessageExKnowShareKey message = new MessageExKnowShareKey();



        knowShare temp = new knowShare();
        temp.setKnowId(knowId);
        temp.setMyStaffCode(myStaffCode);
        temp.setKnowTitle(thisKM.getKnowTitle());
        temp.setKnowCateId(thisKM.getKnowCateId());
        temp.setArchDeptName(thisStaffPost.getArchDeptName());
        temp.setStaffCode4Post(thisStaffPost.getStaffCode());

        if (comment !=null){
            temp.setComment(comment);
        }

//        this.save(temp);

        try{
            this.save(temp);
            message.setCode(200);
            message.setTip("新增成功");
            knowShare checkAfter = this.lambdaQuery().select()
                    .eq(knowShare::getKnowId,knowId)
                    .eq(knowShare::getMyStaffCode,myStaffCode)
                    .one();
            message.setRecId(checkAfter.getRecId());

        }catch (Exception e){
            message.setCode(100);
            message.setTip("新增失败");
        }


        return message;
    }

    @Override
    public IPage<knowShare> getAll(Integer pageNum, Integer pageSize, String knowTitle, Integer knowCateId, String myStaffCode) {
        IPage<knowShare> page = new Page<>(pageNum, pageSize);
        page = this.lambdaQuery().select()
                .like(knowTitle !=null && !knowTitle.isEmpty(), knowShare::getKnowTitle,knowTitle)
                .eq(knowCateId !=null ,knowShare::getKnowCateId,knowCateId)
                .eq(myStaffCode !=null && !myStaffCode.isEmpty(),knowShare::getMyStaffCode,myStaffCode)
                .orderByDesc(knowShare::getCreatetime)
                .page(page);
        return page;
    }

    @Override
    public Message deleteBykey(Integer recId) {
        Message message = new Message();
        QueryWrapper<knowShare> wrapper = new QueryWrapper<>();
        wrapper.eq("\"recId\"",recId);
        try{
            this.remove(wrapper);
            message.setCode(200);
            message.setTip("删除成功");
        }catch (Exception e){
            message.setCode(100);
            message.setTip("删除失败");
        }

        return message;
    }

    @Override
    public Message praise(Integer recId) {
        Message mes = new Message();

        this.lambdaUpdate()
                .eq(knowShare::getRecId,recId)
                .set(knowShare::getPraise,1)
                .update();

        mes.setCode(200);


        return mes;
    }

    @Override
    public knowShare getByRecId(Integer recId) {
        return this.lambdaQuery().select().eq(knowShare::getRecId,recId).one();
    }

    @Override
    public Message updateSimple(Integer recId, Integer readState, String comment) {
        Message message = new Message();
        this.lambdaUpdate().eq(knowShare::getRecId,recId)
                .set(readState != null ,knowShare::getReadState,readState)
                .set(comment != null && !comment.isEmpty(),knowShare::getComment,comment)
                .update();

        message.setCode(200);
        message.setTip("修改成功");

        return message;
    }

    @Override
    public List<knowShare> getByKnowId(Integer knowId) {
        return this.lambdaQuery().select().eq(knowShare::getKnowId,knowId).list();
    }

    @Override
    public void praiseByKS(Integer knowId, String myStaffCode) {
//        Message mes = new Message();

        this.lambdaUpdate()
                .eq(knowShare::getKnowId,knowId)
                .eq(knowShare::getMyStaffCode,myStaffCode)
                .set(knowShare::getPraise,1)
                .update();

//        mes.setCode(200);


//        return mes;
    }

    @Override
    public void updateCommentByKS(Integer knowId, String myStaffCode, String comment) {
        this.lambdaUpdate()
                .eq(knowShare::getKnowId,knowId)
                .eq(knowShare::getMyStaffCode,myStaffCode)
                .set(comment != null && !comment.isEmpty(),knowShare::getComment,comment)
                .update();
    }

    @Override
    public void updateReadStateByKS(Integer knowId, String myStaffCode) {
        this.lambdaUpdate()
                .eq(knowShare::getKnowId,knowId)
                .eq(knowShare::getMyStaffCode,myStaffCode)
                .set(knowShare::getReadState,3)
                .update();
    }

    @Override
    public knowShare getItemBy_KnowId_myStaffCode(Integer knowId, String myStaffCode) {
        return this.lambdaQuery().eq(knowShare::getKnowId,knowId).eq(knowShare::getMyStaffCode,myStaffCode).one();
    }

    @Override
    public List<knowShare> getByMyStaffCode(String myStaffCode) {
        return this.lambdaQuery().eq(knowShare::getMyStaffCode,myStaffCode).list();
    }
}
